﻿using NTLib.Core.Pool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace NTLib.Core.Reflection
{
    /// <summary>
    /// 
    /// </summary>
    /// <seealso cref="NTLib.Core.Reflection.IElementDescriptor" />
    [DataContract]
    public class ValueDescriptor : ObjectDescriptor
    {
        #region Fields

        private static readonly ObjectPool<ValueDescriptor> s_pool = new ObjectPool<ValueDescriptor>();

        #endregion

        #region Properties

        /// <summary>
        /// Gets the pool.
        /// </summary>
        public static new IPool Pool
        {
            get { return s_pool; }
        }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        public string Value { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Jsons the string.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="lvl">The level.</param>
        /// <param name="displayNull">if set to <c>true</c> [display null].</param>
        public override void JsonString(StringBuilder builder, int lvl = 0, bool displayNull = false)
        {
            if (displayNull == false && this.Value == ObjectDescriptor.NULL_STR)
                return;

            builder.Append(' ', lvl * 4);
            if (!string.IsNullOrEmpty(Name))
            {
                builder.Append(Name);
                builder.Append(" : ");
            }

            builder.Append("\"");
            builder.Append(Value);
            builder.Append("\"");
        }

        /// <summary>
        /// Called when the pool item is reset.
        /// Everything MUST be initialized on this method.
        /// </summary>
        protected override void OnReset()
        {
            this.Value = null;
            base.OnReset();
        }

        #endregion
    }
}
